package com.tcs.utils;

import java.util.ArrayList;

public class GeneralUtils {

	public static double[] getCentroid(ArrayList<double[]> aCluster) {

		if (aCluster == null || aCluster.size() == 0) {
			return null;
		}

		double[] centroid = new double[aCluster.get(0).length];
		double[] sumX = null;

		for (double[] nextPoint : aCluster) {
			sumX = getLinearSum(nextPoint, sumX);
		}
		int N = aCluster.size();
		for (int i = 0; i < sumX.length; i++) {
			centroid[i] = sumX[i] / N;
		}
		return centroid;
	}

	public static double[] getLinearSum(double[] aPoint, double[] bPoint) {
		if (aPoint == null) {
			return bPoint;
		}
		if (bPoint == null) {
			return aPoint;
		}

		double[] linearSum = new double[aPoint.length];
		for (int i = 0; i < aPoint.length; i++) {
			linearSum[i] = aPoint[i] + bPoint[i];
		}
		return linearSum;
	}

	public static double getEuclideanDistance(double[] a, double[] b) {
		double sum = 0;
		for (int i = 0; i < a.length; i++) {
			sum = sum + Math.pow((a[i] - b[i]), 2);
		}
		return Math.sqrt(sum);
	}
}
